import { getChatListAction, saveChatMsgAction } from "../services/chat";
export default {
  namespace: 'chat',
  state: {
    chatList: []
  },
  effects: {
    *getChatList ({ payload }, { call, put }) {
      const res = yield call(getChatListAction, payload);
      const { code, msg, data } = res.data;
      if (code === 200) {
        yield put({
          type: 'updateChat',
          payload: {
            chatList: data
          }
        });
      } else {
        // error info
        console.log(msg);
      }
    },
    *saveChatMsg ({ payload, cb }, { call, put }) {
      const res = yield call(saveChatMsgAction, payload);
      const { code, msg } = res.data;
      if (code === 200) {
        yield put({
          type: 'getChatList'
        });
        cb && cb();
      } else {
        // error info
        console.log(msg);
      }
    }
  },
  reducers: {
    updateChat (state, { payload }) {
      return { ...state, ...payload };
    }
  }
}